using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using DHTMLX.Scheduler;
using DHTMLX.Common;
using DHTMLX.Scheduler.Data;
using DHTMLX.Scheduler.Controls;

using scheduleMVC.Models;
using System.Globalization;
using scheduleMVC.DAL;
namespace scheduleMVC.Controllers
{
    public class CalendarController : Controller
    {
        public ActionResult Index()
        {
            //Being initialized in that way, scheduler will use CalendarController.Data as a the datasource and CalendarController.Save to process changes
            //var scheduler = new DHXScheduler(this);
            var scheduler = new DHXScheduler();
            scheduler.DataAction = Url.Action("Data");
            scheduler.SaveAction = Url.Action("Save");
            /*
             * It's possible to use different actions of the current controller
             *      var scheduler = new DHXScheduler(this);     
             *      scheduler.DataAction = "ActionName1";
             *      scheduler.SaveAction = "ActionName2";
             * 
             * Or to specify full paths
             *      var scheduler = new DHXScheduler();
             *      scheduler.DataAction = Url.Action("Data", "Calendar");
             *      scheduler.SaveAction = Url.Action("Save", "Calendar");
             */

            /*
             * The default codebase folder is ~/Scripts/dhtmlxScheduler. It can be overriden:
             *      scheduler.Codebase = Url.Content("~/customCodebaseFolder");
             */


            //scheduler.Config.xml_date = "M/d/yyyy h:mm:ss tt";
            //scheduler.InitialDate = new DateTime(2012, 09, 03);

            scheduler.LoadData = true;
            scheduler.EnableDataprocessor = true;

            return View(scheduler);
        }

        public ContentResult Data()
        {
            var data = new SchedulerAjaxData(
                    new List<CalendarEvent>{ 
                        new CalendarEvent{
                            id = 1, 
                            text = "Sample Event", 
                            start_date = new DateTime(2014, 04, 05, 6, 00, 00), 
                            end_date = new DateTime(2014, 04, 05, 8, 00, 00)
                        },
                        new CalendarEvent{
                            id = 2, 
                            text = "New Event", 
                            start_date = new DateTime(2014, 04, 10, 9, 00, 00), 
                            end_date = new DateTime(2014, 04, 12, 12, 00, 00)
                        },
                        new CalendarEvent{
                            id = 3, 
                            text = "Multiday Event", 
                            start_date = new DateTime(2014, 04, 15, 10, 00, 00), 
                            end_date = new DateTime(2014, 04, 30, 12, 00, 00)
                        }
                    }
                );

            //var myTasks = new TasksContext();
            //DateTime dt1; DateTime dt2;
            //String str1;
            //String str2;
            //String format = "yyyy/mm/dd hh:mm:ss";
            //List<CalendarEvent> myevents = new List<CalendarEvent>();
            //foreach (var r in myTasks.Tasks)
            //{
            //    str1 = r.StartDate.ToString(format);
            //    dt1 = DateTime.ParseExact(str1, format, null);
            //    str2 = r.DueDate.Value.ToString(format);
            //    dt2 = DateTime.ParseExact(str2, format, null);
            //    myevents.Add(new CalendarEvent()
            //    {
            //        id = Convert.ToInt32(r.TasksID),
            //        text = r.Name,
            //        start_date = dt1,
            //        end_date = dt2
            //        //start_date = DateTime.ParseExact(r.StartDate.ToString(), "%m/%d/%Y %H:%i", null, DateTimeStyles.None),
            //        //end_date = DateTime.ParseExact(r.DueDate.ToString(), "%m/%d/%Y %H:%i", null, DateTimeStyles.None)
            //    });

            //}
            //var data = new SchedulerAjaxData(myevents);








            

            return Content(data, "text/json");//note different content types
        }

        public ContentResult Save(int? id, FormCollection actionValues)
        {
            var action = new DataAction(actionValues);
            
            try
            {
                var changedEvent = (CalendarEvent)DHXEventsHelper.Bind(typeof(CalendarEvent), actionValues);

     

                switch (action.Type)
                {
                    case DataActionTypes.Insert:
                        //do insert
                        action.TargetId = changedEvent.id;//assign postoperational id
                        break;
                    case DataActionTypes.Delete:
                        //do delete
                        break;
                    default:// "update"                          
                        //do update
                        break;
                }
            }
            catch
            {
                action.Type = DataActionTypes.Error;
            }
            //return (ContentResult)new AjaxSaveResponse(action);
            return Content(new AjaxSaveResponse(action), "text/xml");
        }
    }
}

